﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyManagementDataPump : ICompanyManagementAccess
    {
        public BaseConfigDataPack GetBaseConfigModel(string copname)
        {
            BaseConfigDataPack pack = new BaseConfigDataPack();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var about = (from c in ctx.tb_about
                              where c.c_vc_cpname == copname
                              select new
                              {
                                  c.c_vc_QQ1,
                                  c.c_vc_QQ2,
                                  c.c_nvc_ICP,
                                  c.c_vc_email,
                              }).FirstOrDefault();
                if (about != null)
                {
                    pack.QQ1 = about.c_vc_QQ1;
                    pack.QQ2 = about.c_vc_QQ2;
                    pack.ICP = about.c_nvc_ICP;
                    pack.EMail = about.c_vc_email;
                }
                var config = (from c in ctx.tb_complanysetting
                              where c.c_vc_cpname == copname
                              select new { 
                                c.c_nvc_keyword,
                                c.c_nvc_title
                              }).FirstOrDefault();
                if (config != null)
                {
                    pack.KeyWord = config.c_nvc_keyword;
                    pack.Title = config.c_nvc_title;
                }
            }
            using (ORM.mysql_empiresEntities ctx = new ORM.mysql_empiresEntities())
            {
                var company = (from c in ctx.tb_company
                               where c.c_vc_cpname == copname
                               select new
                               {
                                   c.c_nvc_name,
                                   c.c_i_industry
                               }).FirstOrDefault();
                if (company != null)
                {
                    pack.Main = company.c_nvc_name;
                    pack.Calling = company.c_i_industry;
                }
            }
            return pack;
        }


        public bool UpdateBaseConfig(string copname, BaseConfigDataPack model)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var about = ctx.tb_about.Where(c => c.c_vc_cpname == copname).FirstOrDefault();
                if (about != null)
                {
                    about.c_vc_QQ1 = model.QQ1;
                    about.c_vc_QQ2 = model.QQ2;
                    about.c_nvc_ICP = model.ICP;
                    about.c_vc_email = model.EMail;
                }
                var config = ctx.tb_complanysetting.Where(c => c.c_vc_cpname == copname).FirstOrDefault();
                if (config != null)
                {
                    config.c_nvc_keyword = model.KeyWord;
                    config.c_nvc_title = model.Title;
                }
                ctx.SaveChanges();
            }
            using (ORM.mysql_empiresEntities ctx = new ORM.mysql_empiresEntities())
            {
                var company = ctx.tb_company.Where(c => c.c_vc_cpname == copname).FirstOrDefault();
                if (company != null)
                {
                    company.c_nvc_name = model.Main;
                    company.c_i_industry = model.Calling;
                }
                ctx.SaveChanges();
            }
            return true;
        }
    }
}
